﻿using System;

namespace Uranus.Common
{
    public class DateCommon
    {
        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 把时间转换昨今明类型
        /// </summary>
        /// <param name="now"></param>
        /// <returns></returns>
        public static string ConvertDateStr(DateTime time, string strFormat)
        {
            return ConvertDateStr(time, strFormat, string.Empty);
        }

        /// <summary>
        /// 把时间转换昨今明类型
        /// </summary>
        /// <param name="now"></param>
        /// <returns></returns>
        public static string ConvertDateStr(DateTime time, string strFormat, string defaultValue)
        {
            if (time.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return defaultValue;
            }
            DateTime now = DateTime.Now;
            int days = (time.Date - now.Date).Days;
            //Convert.ToDateTime(time.ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")))
            //int days = time.Subtract(now).Days;
            if (days > 0)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == -1)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == 0)
            {
                return "今天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else
            {
                return time.ToString(strFormat);
            }
        }

        public static string ConvertDateStr_Year(DateTime time, string strFormat, string strTimeFormat)
        {
            if (time.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return string.Empty;
            }
            DateTime now = DateTime.Now;
            int days = (time.Date - now.Date).Days;
            //Convert.ToDateTime(time.ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")))
            //int days = time.Subtract(now).Days;
            if (days > 0)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == -1)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == 0)
            {
                return "今天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else
            {
                if (time.Year != now.Year)
                {
                    return time.ToString(strFormat);
                }
                else
                {
                    return time.ToString(strTimeFormat);
                }
            }
        }

        public static string ConvertDateStr_Year(DateTime time, string strFormat)
        {
            if (time.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return string.Empty;
            }
            DateTime now = DateTime.Now;
            int days = (time.Date - now.Date).Days;
            //Convert.ToDateTime(time.ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")))
            //int days = time.Subtract(now).Days;
            if (days > 0)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == -1)
            {
                return "昨天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else if (days == 0)
            {
                return "今天 " + time.Hour.ToString().PadLeft(2, '0') + ":" + time.Minute.ToString().PadLeft(2, '0');
            }
            else
            {
                if (time.Year != now.Year)
                {
                    return time.ToString(strFormat);
                }
                else
                {
                    return time.Month + "月" + time.Day + "日";
                }
            }
        }

        /// <summary>
        /// 生成13位时间戳
        /// </summary>
        /// <param name="timeStamp">时间格式</param>
        /// <returns>13位时间戳格式</returns>
        public static string ConvertDateTimeString(DateTime dt)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (dt.Ticks - startTime.Ticks) / 10000;
            return t.ToString();
        }
        /// <summary>
        /// 生成13位时间戳
        /// </summary>
        /// <param name="timeStamp">时间格式</param>
        /// <returns>13位时间戳格式</returns>
        public static string ConvertDateTimeString()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (DateTime.Now.Ticks - startTime.Ticks) / 10000;
            return t.ToString();
        }
    }
}